
///Normale
varying vec4 normal;
///Posizione
varying vec4 point;

///Osservatore
uniform vec4 viewpoint;

void main(void)
{
    int i;

    if ( gl_FragCoord.z < 0.8 ) {
        vec4 bg = vec4(0.0, 0.0, 0.0, 0.0);
        gl_FragColor = bg;
    }
    else {
        vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);
        vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);
        vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);


        vec4 n = normalize(normal);
        vec4 v = normalize(viewpoint - point);
        float shininess = 8.0;
        ///Componente ambientale
        for ( i = 0; i < 1; i ++ ) {
            ambient += gl_LightSource[i].ambient * gl_Color;
            ///Componente diffusiva
            vec4 l = normalize(gl_LightSource[i].position - point);
            float NdotL = dot(n,l);
            diffuse += gl_LightSource[i].diffuse * gl_Color * NdotL;
            ///Componente speculare
            ///vec4 h = normalize(l + v);
            vec3 hv = gl_LightSource[0].halfVector.xyz;
            vec4 h = vec4(hv.x, hv.y, hv.z, 1.0);
            h = normalize(h);
            //if ( NdotL > 0.0 )
            specular += gl_LightSource[0].specular * gl_Color * pow(max(dot(n,h),0.0), shininess);
        }

	gl_FragColor = ambient + diffuse + specular;
    }

}
